package com.test.service.Impl;

import com.test.Aspect.BusinessException;
import com.test.dao.CuisineMapper;
import com.test.domain.Cuisine;
import com.test.domain.CuisineExample;
import com.test.enums.Page;
import com.test.enums.Replay;
import com.test.enums.ReplayCode;
import com.test.service.CuisineService;
import com.test.utils.ReplayUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * CuisteenServiceImpl
 *
 * @author HY
 * 2021/05/24 8:43
 **/


@Service
public class CuisteenServiceImpl implements CuisineService {

    @Resource
    private CuisineMapper cuisineMapper;

    @Override
    public Replay insert(Cuisine cuisine) {

        if (cuisine.getName() == null || cuisine.equals("")){
            throw new BusinessException(ReplayCode.REQUIREDNULL_ERR);
        }

        int rs = cuisineMapper.insert(cuisine);

        if (rs < 1 ){
            throw new BusinessException(ReplayCode.DO_ERR);
        }
        return ReplayUtil.success();
    }

    @Override
    public Replay del(int id) {
        // TODO 删除待完成
        return ReplayUtil.success();
    }

    @Override
    public Replay update(Cuisine cuisine) {

        if (cuisine.getId() == null || cuisine.getId().equals("")){
            throw new BusinessException(101,"必填参数不能为空");
        }

        CuisineExample cuisineExample = new CuisineExample();
        CuisineExample.Criteria criteria = cuisineExample.createCriteria();

        criteria.andIdEqualTo(cuisine.getId());

        int rs = cuisineMapper.updateByExampleSelective(cuisine,cuisineExample);

        if (rs < 1){
            throw new BusinessException(ReplayCode.DO_ERR);
        }

        return ReplayUtil.success();
    }

    @Override
    public Replay select(Cuisine cuisine, Page page) {

        if (page.isNull()){
            //抛出空值异常
            throw new BusinessException(ReplayCode.Page_NULL);
        }

        CuisineExample cuisineExample = new CuisineExample();
        CuisineExample.Criteria criteria = cuisineExample.createCriteria();

        if (cuisine.getName().equals("") || cuisine.getName() != null) {
            criteria.andNameLike("%"+cuisine.getName()+"%");
        }
        cuisineExample.setPage(page.getPage());
        cuisineExample.setLimit(page.getLimit());

        List<Cuisine> cuisines = cuisineMapper.selectByExample(cuisineExample);
        int count = (int) cuisineMapper.countByExample(cuisineExample);

        return ReplayUtil.success("查询成功", cuisines, count);
    }
}
